package com.hckjsec.oa.controller;

import java.util.ArrayList;
import java.util.List;

import com.hckjsec.common.core.redis.SerialNumberUtils;
import com.hckjsec.common.enums.NumbereEnum;
import com.hckjsec.common.utils.SecurityUtils;
import com.hckjsec.common.utils.StringUtils;
import com.hckjsec.framework.aspectj.DataScopeAspect;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.hckjsec.common.annotation.Log;
import com.hckjsec.common.core.controller.BaseController;
import com.hckjsec.common.core.domain.AjaxResult;
import com.hckjsec.common.enums.BusinessType;
import com.hckjsec.oa.domain.OaContract;
import com.hckjsec.oa.service.IOaContractService;
import com.hckjsec.common.utils.poi.ExcelUtil;
import com.hckjsec.common.core.page.TableDataInfo;

/**
 * 公司OA合同Controller
 * 
 * @author cyt
 * @date 2022-01-10
 */
@RestController
@RequestMapping("/oa/contract")
public class OaContractController extends BaseController
{
    @Autowired
    private IOaContractService oaContractService;

    /**
     * 查询公司OA合同列表
     */
    @GetMapping("/list")
    public TableDataInfo list(OaContract oaContract)
    {
        startPage();
        oaContract.getParams().put("authority", DataScopeAspect.getDataScopeSql(SecurityUtils.getSysUser()));
        List<OaContract> list = oaContractService.selectOaContractList(oaContract);
        return getDataTable(list);
    }

    /**
     * 导出公司OA合同列表
     */
    @Log(title = "公司OA合同", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(OaContract oaContract)
    {
        List<OaContract> list = oaContractService.selectOaContractList(oaContract);
        ExcelUtil<OaContract> util = new ExcelUtil<OaContract>(OaContract.class);
        return util.exportExcel(list, "contract");
    }

    /**
     * 获取公司OA合同详细信息
     */
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(oaContractService.selectOaContractById(id));
    }

    /**
     * 新增公司OA合同
     */
    @Log(title = "公司OA合同", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody OaContract oaContract)
    {
        //生成合同编号
        String identifier = StringUtils.isHcNull(oaContract.getIdentifier(),"");
        if ("".equals(identifier)){
            if ("销售合同".equals(oaContract.getType())){
                identifier = SerialNumberUtils.getSerialNumber(NumbereEnum.SYS_APPLETS_VENDITION_NUMBER);
            }else{
                identifier = SerialNumberUtils.getSerialNumber(NumbereEnum.SYS_APPLETS_PURCHASE_NUMBER);
            }
            //加横杆
            List<String> numberList= new ArrayList<>();
            numberList.add(identifier.substring(0,4));
            numberList.add(identifier.substring(4,12));
            numberList.add(identifier.substring(12));
            identifier = StringUtils.join(numberList, "-");
        }
        oaContract.setIdentifier(identifier);
        oaContract.setUserName(SecurityUtils.getNickName());
        oaContract.setDeptId(SecurityUtils.getDeptId());
        oaContract.setUserId(SecurityUtils.getUserId());
        oaContract.setCreateBy(SecurityUtils.getUsername());
        return toAjax(oaContractService.insertOaContract(oaContract));
    }

    /**
     * 修改公司OA合同
     */
    @Log(title = "公司OA合同", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody OaContract oaContract)
    {
        oaContract.setUpdateBy(SecurityUtils.getUsername());
        return toAjax(oaContractService.updateOaContract(oaContract));
    }

    /**
     * 删除公司OA合同
     */
    @Log(title = "公司OA合同", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(oaContractService.deleteOaContractByIds(ids));
    }
}
